package cn.yusei.teamTribe.service.post;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.yusei.teamTribe.constant.PermissionType;
import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.dao.post.PostDAO;
import cn.yusei.teamTribe.dao.post.PostReflecDAO;
import cn.yusei.teamTribe.dao.post.po.PostPO;
import cn.yusei.teamTribe.dao.post.po.PostReflectPO;
import cn.yusei.teamTribe.entity.Post;
import cn.yusei.teamTribe.exception.PermissionForbiddenException;
import cn.yusei.teamTribe.exception.PostNotFoundException;
import cn.yusei.teamTribe.service.PermissionService;

@Service
@Transactional
public class PostService {

	@Autowired
	private PostDAO postDAO;
	@Autowired
	private PostReflecDAO postReflecDAO;
	@Autowired
	private PermissionService permissionService;

	/**
	 * 
	 * @param userId
	 * @param teamId
	 * @param post
	 * @return 返回新增 post 的 id
	 */
	public int add(int userId, int teamId, Post post) throws PermissionForbiddenException, PostNotFoundException {
		// 发帖的时候一律按普通帖处理,不添加任何标签
		if (post.getTargetId() == 0) {
			//发帖，teamId等于表示在 team 外
			if (teamId != 0 && !permissionService.havePermission(userId, teamId, PermissionType.SET_TEAM_NORMAL_POST, 0))
				throw new PermissionForbiddenException("用户 [" + userId + "] 没有设置社团 [" + teamId + "] 普通帖子的权利");
		}
		else {
			//评论
			if(!postDAO.exists(post.getTargetId()))
				throw new PostNotFoundException();
		}
		PostPO postPO = new PostPO();
		BeanUtils.copyProperties(post, postPO);
		PostReflectPO postReflectPO = new PostReflectPO();
		postReflectPO.setPostId(post.getId());
		postReflectPO.setTeamId(teamId);
		postReflectPO.setUserId(userId);
		postDAO.insert(postPO);
		postReflecDAO.updateReflect(postReflectPO);
		return postDAO.selectLastInertId();
	}

	public int delete(int userId, int postId) {
		if (!postDAO.exists(postId))
			return ResponseCode.POST_NOT_EXISTS;
		if (postReflecDAO.selectUserById(postId) != userId && !permissionService.havePostPermission(userId, postId, false))
			return ResponseCode.PERMISSION_FORBIDDEN;
		postDAO.delete(postId);
		return ResponseCode.REQUEST_SUCCESSED;
	}

	public int update(int userId, Post post) {
		if (!postDAO.exists(post.getId()))
			return ResponseCode.POST_NOT_EXISTS;
		if (postReflecDAO.selectUserById(post.getId()) != userId
				&& !permissionService.havePostPermission(userId, post.getId(), false))
			return ResponseCode.PERMISSION_FORBIDDEN;
		PostPO postPO = new PostPO();
		BeanUtils.copyProperties(post, postPO);
		postDAO.update(postPO);
		return ResponseCode.REQUEST_SUCCESSED;
	}

	public Post get(int id) throws PostNotFoundException {
		if (!postDAO.exists(id))
			throw new PostNotFoundException("帖子 [" + id + "] 不存在");
		PostPO postPO = postDAO.selectById(id);
		Post post = new Post();
		BeanUtils.copyProperties(postPO, post);
		return post;
	}

	/**
	 * 获取用户 userId 的第 offset+1 到 offset + num 个帖子
	 * 
	 * @param userId
	 * @param offset
	 * @param num
	 * @return
	 */
	public int[] getByUserId(int userId, int offset, int num) {
		int[] ids = null;
		ids = postDAO.selectByUserId(userId, offset, num);
		return ids;
	}

	public int[] getByTargetId(int targetId, int offset, int num) {
		int[] ids = null;
		ids = postDAO.selectByTargetId(targetId, offset, num);
		return ids;
	}

	public int[] getByTeam(int teamId, int offset, int num) {
		int[] ids = null;
		ids = postDAO.selectByTeam(teamId, offset, num);
		return ids;
	}
}
